package org.yoao.moneymachine.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.yoao.crm.DBManager.DBManager;
import org.yoao.moneymachine.bean.File;

/**
 * @author  Weishion.Yi
 * @description
 * @date 2010-10-4 下午03:12:13
 */
public class FileDAO extends DBManager {
	private static final Logger log = Logger.getLogger(FileDAO.class);

	public int getDataCount(Map map) throws Exception{
		String sql = "select count(id) as fileCount from mmfile";
		String where = " where ";
		if(map != null && !map.isEmpty()){
			Iterator iter = map.keySet().iterator();
			while(iter.hasNext()){
				String key = iter.next().toString();
				String value = map.get(key).toString();
				where = where + key + "='" + value + "' and ";
			}
		}
		if(where.endsWith(" and ")) where = where.substring(0, where.length()-4);
		if(where.equals(" where ")) where = "";
		sql += where;
		String result = querySingleValue(sql);
		if(result == null || result.trim().equals("")) result = "0";
		return Integer.parseInt(result);
	}


	public List find(int startRow, int endRow, Map map) throws Exception{
		String sql = "select top "+endRow+" mmfile.id, mmfile.fileName, mmfile.fileUrl, mmfile.accessSno," +
					 "mmfile.createTime from mmfile " +
					 "where mmfile.id not in(";
		String filterSql = "select top "+startRow+" f2.id from mmfile as f2 ";

		String where = "";
		String fwhere = "";
		if(map != null && !map.isEmpty()){
			Iterator iter = map.keySet().iterator();
			while(iter.hasNext()){
				String key = iter.next().toString();
				String value = map.get(key).toString();
				where = where + "mmfile." + key + "='" + value + "' and ";
				fwhere = fwhere + "f2." + key + "='" + value + "' and ";
			}
		}
		if(where.endsWith(" and ")) where = where.substring(0, where.length()-4);
		if(where.equals("")) where = null;
		if(fwhere.endsWith(" and ")) fwhere = fwhere.substring(0, fwhere.length()-4);
		if(fwhere.equals("")) fwhere = null;

		if(where == null){
			fwhere = " order by f2.id desc";
			sql = sql + filterSql + fwhere + ") order by mmfile.id desc";
		}else{
			filterSql = filterSql + " where " + fwhere + " order by f2.id desc";
			sql = sql + filterSql + ") and " + where + " order by mmfile.id desc";
		}
		return find(sql);
	}

	public List findAll() throws Exception{
		String sql = "select id, fileName, fileUrl, accessSno, createTime from mmfile";
		return find(sql);
	}

	public File findById(String id) throws Exception{
		String sql = "select id, fileName, fileUrl, accessSno, createTime from mmfile where id="+id;
		return findSingle(querySingleRow(sql));
	}

	private List find(String sql) throws Exception{
		List list = queryMultiColumn(sql);
		List result = null;
		if(list != null && !list.isEmpty()){
			result = new ArrayList();
			for(int i=0;i<list.size();i++){
				Map map = (Map)list.get(i);
				File file = findSingle(map);
				if(file == null) file = new File();
				result.add(file);
			}
		}
		return result;
	}

	private File findSingle(Map data) throws Exception{
		File file = null;
		if(data != null && !data.isEmpty()){
			file = new File();
			file.setId(data.get("ID").toString());
			file.setFileName((String)data.get("FILENAME"));
			file.setFileUrl((String)data.get("FILEURL"));
			file.setAccessSno((String)data.get("ACCESSSNO"));
			file.setCreateTime((String)data.get("CREATETIME"));
		}
		return file;
	}

	public int insert(File file) throws Exception{
		String sql = "insert into mmfile(fileName, fileUrl, accessSno, createTime)values('"+
					 file.getFileName()+"', '"+file.getFileUrl()+"', '"+file.getAccessSno()+"', '"+
					 file.getCreateTime()+"')";
		return update(sql);
	}

	public int delete(String id) throws Exception{
		String sql = null;
		if(id.indexOf(",") > 0)
			sql = "delete from mmfile where id in("+id+")";
		else
			sql = "delete from mmfile where id="+id;
		return update(sql);
	}
}